package com.GUIWork;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;

import com.dbConnection.theConnection;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class newUserFrame extends JFrame {
	private JTextField fName;
	private JTextField lName;
	private JTextField username;
	private JTextField pass;
	private JTextField confirm;
	private newUserFrame uframe = this;
	theConnection conn;
	
	public newUserFrame(final theConnection conn) {
		
		this.conn = conn;
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		
		JSplitPane splitPane = new JSplitPane();
		panel.add(splitPane);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LoginFrame lframe = new LoginFrame(conn);
				lframe.setVisible(true);
				lframe.setBounds(100, 100, 450, 300);
				lframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				uframe.dispose();
			}
		});
		splitPane.setRightComponent(btnCancel);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(null);
		
		JLabel label = new JLabel("");
		label.setBounds(188, 179, 46, 14);
		panel_1.add(label);
		
		fName = new JTextField();
		fName.setBounds(193, 40, 86, 20);
		panel_1.add(fName);
		fName.setColumns(10);
		
		JLabel lblPleaseEnterThe = new JLabel("Please Enter the Following Information");
		lblPleaseEnterThe.setBounds(109, 11, 224, 14);
		panel_1.add(lblPleaseEnterThe);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(84, 99, 99, 14);
		panel_1.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(84, 130, 86, 14);
		panel_1.add(lblPassword);
		
		lName = new JTextField();
		lName.setBounds(193, 65, 86, 20);
		panel_1.add(lName);
		lName.setColumns(10);
		
		username = new JTextField();
		username.setBounds(193, 96, 86, 20);
		panel_1.add(username);
		username.setColumns(10);
		
		pass = new JTextField();
		pass.setBounds(193, 127, 86, 20);
		panel_1.add(pass);
		pass.setColumns(10);
		
		confirm = new JTextField();
		confirm.setBounds(193, 158, 86, 20);
		panel_1.add(confirm);
		confirm.setColumns(10);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(84, 43, 99, 14);
		panel_1.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(84, 74, 99, 14);
		panel_1.add(lblLastName);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password");
		lblConfirmPassword.setBounds(55, 161, 115, 14);
		panel_1.add(lblConfirmPassword);
		
		final JLabel Response = new JLabel("");
		Response.setBounds(193, 179, 86, 14);
		panel_1.add(Response);
		
		JPanel panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.NORTH);
		
		JLabel lblWelcomeNewUser = new JLabel("Welcome New User!");
		lblWelcomeNewUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(lblWelcomeNewUser);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(username.getText().length()!=0 && fName.getText().length()!=0
						&& lName.getText().length()!=0 && pass.getText().length()!=0
						&& confirm.getText().length()!=0){
					if(pass.getText().equals(confirm.getText())){
						String firstName = fName.getText();
						String lastName = lName.getText();
						String user = username.getText();
						String pw = pass.getText();
						String div = ", ";
						try{
							conn.updateDB("INSERT INTO User (Username, First, Last, Password) VALUES " +
									"(" + user + div + firstName + lastName + div + pw + ")");
							Response.setText("User Created");
						}catch(SQLException e){
							Response.setText("User Creation Failed");
						}
					}
					else
						Response.setText("Passwords do not match");
				}
				else
					Response.setText("Please fill in all fields");
			}
		});
		splitPane.setLeftComponent(btnSubmit);
	}
}
